﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/SiteMaster.Master"
    Inherits="System.Web.Mvc.ViewPage<Telerik.Web.Mvc.GridModel>" %>

<%@ Import Namespace="Microhard.Framework.MVCHelper" %>
<%@ Import Namespace="Microhard.Permission.Interface" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    功能授权
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="section-header">
        <div class="title">
            <img src="<%=Url.Content("~/Content/images/ico-catalog.png")%>" alt="" />
            功能授权
        </div>
        <div class="options">
        </div>
    </div>
    <table width="100%">
        <tr>
            <td class="adminTitle">
                <img alt="类型" src="<%=Url.Content("~/Content/images/ico-help.gif") %>" title="类型"></img><label
                    for="operationName" title="类型">类型</label>:
            </td>
            <td class="adminData">
                <%var items = new List<SelectListItem>();
                  items.Add(new SelectListItem
                  {
                      Text = "请选择",
                      Value = "-1"
                  });
                  items.Add(new SelectListItem
                  {
                      Text = "角色",
                      Value = ((int)Microhard.Core.Enums.OwnerIDType.Role).ToString()
                  });
                  items.Add(new SelectListItem
                  {
                      Text = "用户",
                      Value = ((int)Microhard.Core.Enums.OwnerIDType.User).ToString()
                  });
                %>
                <%=Html.DropDownList("TypeSelect", items)%>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                <img alt="角色" src="<%=Url.Content("~/Content/images/ico-help.gif") %>" title="角色"></img><label
                    for="operationName" title="角色">角色</label>:
            </td>
            <td class="adminData">
                <%Html.RenderAction("GetSelectList", "Role", new Microhard.WebUI.Models.AttriubteOfSelectList { ParameterModelName = "SelectedRoleID", IsMulitiplySelect = false, DefaultSelectedText = "请选择", DefaultSelectedValue = Guid.Empty.ToString() });%>
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                <%Html.Telerik().Grid<Microhard.Permission.Models.Permission>((List<Microhard.Permission.Models.Permission>)this.Model.Data)
                    .Name("us-grid")
                         .ToolBar(t => t.Template(g =>
                    { 
                %>
                <%var searchParms = new Dictionary<string, string>();
                  searchParms.Add("OwnerID", "SelectedRoleID");
                  searchParms.Add("OwnerType", "TypeSelect");
                %>
                <div class="header-menu">
                    <ul class="t-widget t-reset t-header t-menu">
                        <li class="t-item  t-state-default">
                            <%=Html.Authorized_SearchButton("btnSearch", "查询", "us-grid", searchParms, "OperationGridOnGridDataBinding")%>
                        </li>
                        <li class="t-item  t-state-default">
                            <%=Html.Authorized_AddButton("btnAdd", "添加", "Create", "Permission")%>
                        </li>
                        <li class="t-item  t-state-default">
                            <%=Html.Authorized_DeleteButtonWithCallBack("btnDelete", "删除", "DeleteSingle", "Permission", "afterDelete(data)", "SetValueWhenDelete()","Admin")%>
                        </li>
                    </ul>
                </div>
                <%}))
                   .ClientEvents(events => events.OnDataBinding("OperationGridOnGridDataBinding"))
                         .Columns(columns =>
                    {
                        columns.Bound(x => x.OwnerID)
                       .Template(x => Html.CheckBox("checkedRecords", new { value = x.OwnerID, @class = Html.GridSelectingUsedCSS() }))
                       .ClientTemplate("<input type='checkbox' name='checkedRecords' value='<#= OwnerID #>' class='gridCks'/>")
                     .HtmlAttributes(new { style = "text-align:center" }).Title("选择");

                        columns.Bound(x => x.OwnerName).Title("授权人/角色");
                        columns.Bound(x => x.OwnerTypeName).Title("类型");
                        columns.Bound(x => x.ResourceName).Title("资源");
                        columns.Bound(x => x.OperationName).Title("操作");
                        columns.Bound(x => x.BelongGroup).Title("组范围");
                        columns.Bound(x => x.ScopeDisplyName).Title("数据权限");
                        var hdcol = columns.Bound(x => x.OwnerID);
                        hdcol.Template(x => Html.Hidden(x.OwnerID.ToString(), x.ResID))
                            .ClientTemplate(@"<input type=""hidden"" value=""<#= ResID #>"" name=""<#= OwnerID#>"">").Hidden();
                    })
                      .Pageable(settings => settings.Total(Model.Total).PageSize(Html.GetGridPageSize()).Position(GridPagerPosition.Top))
                      //绑定搜索按钮，执行对应的action
                    .DataBinding(dataBinding => dataBinding.Ajax().Select("_List", "Permission"))
                    .EnableCustomBinding(true)
                    .Render();
                %>
            </td>
        </tr>
    </table>
    <script type="text/javascript">
        function afterDelete(data) {
            if (data == 'succeed') {
                var grid = $('#us-grid').data('tGrid');
                grid.currentPage = 1;
                grid.ajaxRequest();
                return;
            }
            alert(data);
        }

        function SetValueWhenDelete() {
            var models = [];
            var selectedList = $(".<%=Html.GridSelectingUsedCSS() %>:checked");
            if (selectedList.length == 0) {
                return models;
            }
            else {
                //所以使用如下方式获得选择的resid
                for (var i = 0; i < selectedList.length; i++) {
                    var hd = $(selectedList[i]).parent().nextAll().get(6).children[0];
                    var matchName = hd.name;
                    if (matchName == selectedList[i].value) {
                        models[i] = { ResID: hd.value, OwnerID: selectedList[0].value };
                    }
                }
            }
            return models;
        }

    </script>
</asp:Content>
